package hr.koshnica.android.ui;

import hr.koshnica.android.R;
import hr.koshnica.android.database.KoshnicaContract.Locations;
import hr.koshnica.android.database.KoshnicaDatabase;
import hr.koshnica.android.database.KoshnicaDatabase.Tables;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LocationEdit extends Activity {

	private KoshnicaDatabase mDbHelper;

	private EditText mNameText;
	private EditText mDescText;
	private EditText mGenreText;
	private Long mRowId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mDbHelper = new KoshnicaDatabase(this);
		mDbHelper.open();

		setContentView(R.layout.location_edit);
		setTitle(R.string.location_edit);

		mNameText = (EditText) findViewById(R.id.locNameLocEdit);
		mDescText = (EditText) findViewById(R.id.locDescLocEdit);
		mGenreText = (EditText) findViewById(R.id.locGenreLocEdit);

		Button confirmButton = (Button) findViewById(R.id.confirmBtnLocEdit);

		mRowId = (savedInstanceState == null) ? null
				: (Long) savedInstanceState
						.getSerializable(BaseColumns._ID);

		populateFields();

		mDbHelper.close();
		confirmButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	private void populateFields() {
		if (mRowId != null) {
			mDbHelper.open();
			Cursor note = mDbHelper.fetchInput(
					Tables.LOCATIONS, mRowId);
			startManagingCursor(note);
			mNameText.setText(note.getString(note
					.getColumnIndexOrThrow(Locations.LOC_NAME)));
			mDescText.setText(note.getString(note
					.getColumnIndexOrThrow(Locations.LOC_DESC)));
			mGenreText.setText(note.getString(note
					.getColumnIndexOrThrow(Locations.LOC_GENRE)));
			mDbHelper.close();
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		saveState();
		outState.putSerializable(BaseColumns._ID, mRowId);
	}

	@Override
	protected void onPause() {
		super.onPause();
		saveState();
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void saveState() {
		String locName = mNameText.getText().toString();
		String locDesc = mDescText.getText().toString();
		String locGenre = mGenreText.getText().toString();
		mDbHelper.open();
		
		ContentValues values = new ContentValues();
		values.put(Locations.LOC_NAME, locName);
		values.put(Locations.LOC_DESC, locDesc);
		values.put(Locations.LOC_GENRE, locGenre);
		values.put(Locations.LOC_STATUS, "NEUTRAL");
		values.put(Locations.LOC_LIKE, 0);
		
		if (mRowId == null) {
			long id = mDbHelper.insertInput(Tables.LOCATIONS, values);
			if (id > 0) {
				mRowId = id;
			}
		} else {
			mDbHelper.updateInput(Tables.LOCATIONS, mRowId, values);
		}
		mDbHelper.close();
	}

}
